package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.CourseSubjectApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.common.util.IdentificationUtil;
import com.example.wisdomclassroomapi.domain.entity.CourseSubjectEntity;
import com.example.wisdomclassroomapi.facade.assembler.CourseSubjectFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.CourseSubjectRequestDto;
import com.example.wisdomclassroomapi.facade.dto.CourseSubjectResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/courseSubject")
@Slf4j
public class CourseSubjectController extends BaseController {
    @Resource
    private CourseSubjectApplicationService courseSubjectApplicationService;

    @Resource
    private CourseSubjectFacadeAssembler courseSubjectFacadeAssembler;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public PageDto<List<CourseSubjectResponseDto>> getCourseSubjectByPage(@RequestBody @Valid CourseSubjectRequestDto request) {
        IPage<CourseSubjectEntity> page = new Page<CourseSubjectEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
        CourseSubjectEntity entity = courseSubjectFacadeAssembler.assembleCourseSubjectEntity(request);
        page = courseSubjectApplicationService.page(entity, page);
        List<CourseSubjectResponseDto> assembleData = new ArrayList<>();
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            for (CourseSubjectEntity item : page.getRecords()) {
                assembleData.add(courseSubjectFacadeAssembler.assembleCourseSubjectResponseDto(item));
            }
        }
        PageDto<List<CourseSubjectResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setTotal(page.getTotal());
        result.setData(assembleData);
        return result;
    }


    /**
     * 查询
     */
    @PostMapping("/list")
    public List<CourseSubjectResponseDto> getCourseSubjects(@RequestBody @Valid CourseSubjectRequestDto request) {
        CourseSubjectEntity entity = courseSubjectFacadeAssembler.assembleCourseSubjectEntity(request);
        List<CourseSubjectEntity> list = courseSubjectApplicationService.list(entity);
        List<CourseSubjectResponseDto> assembleData = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (CourseSubjectEntity item : list) {
                assembleData.add(courseSubjectFacadeAssembler.assembleCourseSubjectResponseDto(item));
            }
        }
        return assembleData;
    }

    /**
     * 获取某一实体
     */
    @PostMapping("/getById")
    public CourseSubjectResponseDto getCourseSubjectDetails(@RequestBody CourseSubjectEntity entity) {
        entity = courseSubjectApplicationService.findById(entity.getId());
        CourseSubjectResponseDto responseDto = null;
        if (entity != null) {
            responseDto = courseSubjectFacadeAssembler.assembleCourseSubjectResponseDto(entity);
        }
        return responseDto;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public void saveCourseSubject(@RequestBody CourseSubjectRequestDto request) {
        CourseSubjectEntity entity = courseSubjectFacadeAssembler.assembleCourseSubjectEntity(request);
        entity.setId(IdentificationUtil.shortUuid());
        courseSubjectApplicationService.add(entity);
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    public void modifyCourseSubject(@RequestBody CourseSubjectEntity entity) {
//        CourseSubjectEntity entity = courseSubjectFacadeAssembler.assembleCourseSubjectEntity(request);
        courseSubjectApplicationService.updateById(entity);
    }

    /**
     * 删除数据
     */
    @PostMapping("/delete")
    public void removeCourseSubject(@RequestBody @Valid String id) {
        courseSubjectApplicationService.deleteById(id);
    }
}